<!--
* description: 国地补总部审核
* author: wangjuan
* createdDate: 2020-11-18
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="false"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />

    <edit
      v-if="editPopupsVisible"
      :key="editPopupsKey"
      :data-for-form="editRowData"
      :is-dialog-visible.sync="editPopupsVisible"
      :title-name="titleName"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/subsidyManage'
import Vue from 'vue'

export default {
  name: 'StateLandSubsidyHQAudit',

  components: {
    OneTableTemplate,
    Edit: () => import('../stateLandSubsidyHQAudit/edit')
  },

  // 组件混入对象：{data[listQuery] methods[queryTable]}
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      // 网格查询API配置对象
      apiConfig: api.queryHqSubsidyAudit,
      editRowData: {},
      rowData: '',
      titleName: '总部审核',

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey4',
          type: '',
          size: 'small',
          name: 'export',
          clickEvent: () => this.export(),
          text: '导出',
          position: 'left'
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '交付门店：',
              codeField: 'dlrShortName',
              textField: 'deliveryDlrId',
              component: () => import('@/views/ve/salesStore/delivery.vue'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '订单编号：',
              codeField: 'subSaleOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '订单标签：',
              codeField: 'saleOrderSign',
              lookuptype: 'VE1013',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '补贴上牌城市：',
              codeField: 'licenseLocationCity',
              oFields: 'cityId,cityName',
              filterable: true,
              customObj: {
                listQuery: {
                  cityList: ''
                },
                api: { APIUrl: '/mp/e3sh/vesale/common/geographicDataService/listOfCities.do' }
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '开票日期：',
              codeField: 'invoiceStartTime,invoiceEndTime',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'daterange',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '申请时间：',
              codeField: 'applyStartTime,applyEndTime',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'daterange',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey8',
              labelName: '门店类别：',
              codeField: 'orgTypeName',
              lookuptype: 'VE1136',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey9',
              labelName: '车牌号码：',
              codeField: 'carLicenseNo',
              validrule: { noRequire: true, iscPattern: true },
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isRequire: true,
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey10',
              labelName: '审核状态：',
              codeField: 'colNtatusName',
              lookuptype: 'VE1086 ',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'subsidyId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'controlBtn',
              label: '操作',
              width: 100,
              fixed: true,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '审核',
                  rowFileds: 'show:colStatusName-区域审核通过',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.edit(index)
                },
                {
                  compKey: 'propKey5',
                  codeField: 'downloadAttachment',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.download(index),
                  labelName: '下载附件'
                }
              ]
            },
            {
              prop: 'dlrShortNameC',
              label: '交付门店'
            },
            {
              prop: 'cityName',
              label: '城市'
            },
            {
              prop: 'bigAreaName',
              label: '大区'
            },
            {
              prop: 'orgTypeName',
              label: '门店类别'
            },
            {
              prop: 'subSaleOrderCode',
              label: '订单编号',
              width: 140
            },
            {
              prop: 'saleOrderName',
              label: '订单标签'
            },
            {
              prop: 'colStatusName',
              label: '审核状态'
            },
            {
              prop: 'buyTypeName',
              label: '购买方式'
            },
            {
              prop: 'custLxName',
              label: '联系人姓名'
            },
            {
              prop: 'custLxPhone',
              label: '联系人电话',
              width: 140
            },
            {
              prop: 'qualifiedCertNo',
              label: '合格证编号',
              width: 140
            },
            {
              prop: 'produceDate',
              label: '车辆制造日期',
              width: 140
            },
            {
              prop: 'carConfigCn',
              label: '车型名称'
            },
            {
              prop: 'provinceName',
              label: '补贴上牌省份'
            },
            {
              prop: 'cityName',
              label: '补贴上牌城市'
            },
            {
              prop: 'invoiceNo',
              label: '发票号码'
            },
            {
              prop: 'invoiceDate',
              label: '开票日期',
              width: 140
            },
            {
              prop: 'vin',
              label: 'VIN码',
              width: 140
            },
            {
              prop: 'invoicePrice',
              label: '开票价格'
            },
            {
              prop: 'dlrFullNameC',
              label: '销货单位名称',
              width: 140
            },
            {
              prop: 'licenseDate',
              label: '行驶证注册日期',
              width: 140
            },
            {
              prop: 'carLicenseNo',
              label: '车牌号'
            },
            {
              prop: 'licenseBelong',
              label: '行驶证所有人'
            },
            {
              prop: 'bandCode',
              label: '品牌型号'
            },
            {
              prop: 'usePropName',
              label: '使用性质'
            },
            {
              prop: 'custTypeName',
              label: '开票人类型'
            },
            {
              prop: 'enterpriseName',
              label: '企业名称'
            },
            {
              prop: 'socialCode',
              label: '统一社会信用代号',
              width: 140
            },
            {
              prop: 'enterprisePhone',
              label: '企业联系电话',
              width: 100
            },
            {
              prop: 'enterpriseAdd',
              label: '企业地址'
            },
            {
              prop: 'corpName',
              label: '法人姓名'
            },
            {
              prop: 'corpCredNo',
              label: '法人身份证号',
              width: 140
            },
            {
              prop: 'corpRepPhone',
              label: '法人联系电话',
              width: 100
            },
            {
              prop: 'corpAddress',
              label: '法人地址',
              width: 140
            },
            {
              prop: 'applyTime',
              label: '申请时间',
              width: 140
            },
            {
              prop: 'applyMen',
              label: '申请人'
            }
          ],
      callbackObj: {
        onCheckSelectChange: (selection) => {
          this.rowData = selection.map((item) => {
            return item.subsidyId
          })
          this.rowData = this.rowData.join()
        }
      },

      // 表单数据
      formField: this.$utils.getFormField(this, {
        dlrShortName: '',
        deliveryDlrId: '',
        subSaleOrderCode: '',
        vin: '',
        saleOrderSign: '',
        licenseLocationCity: '',
        orgTypeName: '',
        carLicenseNo: '',
        colNtatusName: '',
        invoiceStartTime: '',
        invoiceEndTime: '',
        applyStartTime: '',
        applyEndTime: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },
  mounted() {
    // 列表查询
    this.queryTable(1)
  },
  methods: {
    // 下载附件
    download(index) {
      var datalist = this.$refs.multipleTable.list[index]
      var data
      data = [
        datalist.corpFile,
        datalist.invoiceFile,
        datalist.enterpriseFile ? datalist.enterpriseFile : '',
        datalist.drlFile ? datalist.drlFile : ''
      ]
      this.$utils.FileDownload(data)
    },
    // 导出
    export() {
      const fileName = this.$utils.fileNameWithTime('国地补总部审核.xlsx')
      this.$requestAPI({
        apiUrl: api.exporthqSubsidy.APIUrl,
        variables: { subsidyId: this.rowData, ...this.formField, dataType: 'excel' }
      }).then(res => {
        this.$utils.downloadFile(res, fileName)
      }).catch(err => {
        console.log(err)
      })
    },

    edit(index) {
      this.editPopupsVisible = true
      this.editRowData = this.$refs['multipleTable'].list[index]
    },

    close() {
      this.editPopupsVisible = false
      this.queryTable(1)
    }
  }
}
</script>
